﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Maticsoft.Common;
using DQYT.HTDZ.DBUtility;
using DQYT.HTDZ.BusinessService;
using System.Xml;
using System.Text;
using System.Drawing;
using DQYT.HTDZ.Common;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;

namespace DQYT.HTDZ.Web.Views.SJCJDB
{
    public partial class HBYTDATAIN : System.Web.UI.Page
    {
        
        private List<CJSJModel> cjsjModels = new List<CJSJModel>();//存放采集数据集合
        private BDDATESOURSEBLL datesoursebll = new BDDATESOURSEBLL();
        private BDDATESOURCEModel BbDatasourceModel = new BDDATESOURCEModel();
        private CjsjParser cjsjParser;//工具类，转换采集数据
        private DataTable table_gl;
        private DataTable table_gt;
     
        protected void Page_Load(object sender, EventArgs e)
        {
            this.cjsjParser = new CjsjParser();

        }

        protected void hbyt_btn_Click(object sender, EventArgs e)
        {
            string jh;
            string csrq;
            string dla;
            string dlb;
            string dlc;
            string dya;
            string dyb;
            string dyc;
            string yggla;
            string ygglb;
            string ygglc;
            string wggla;
            string wgglb;
            string wgglc;

            string dmzh;
            string dmwy;
            

            DataSet ds = new DataSet();
            string sqlstr = "select * from GL";
            ds = datesoursebll.GetList_SQL(sqlstr);
            table_gl = ds.Tables[0];
            string substr1;
            int index;
            for (int i = 0; i < table_gl.Rows.Count; i++)
            {
                this.BbDatasourceModel = new BDDATESOURCEModel();
                this.cjsjModels = new List<CJSJModel>();
                jh = table_gl.Rows[i]["JH"].ToString();
                csrq = ((DateTime)table_gl.Rows[i]["CSRQ"]).ToString("yyyyMMddHHmmss");
                this.BbDatasourceModel.WELLID = jh;
                csrq = csrq.Replace("/", "");
                csrq = csrq.Replace(":", "");
                csrq = csrq.Replace(" ", "");
                this.BbDatasourceModel.JCRQ = TimeParser.strToDateTime(csrq);
                this.BbDatasourceModel = transBDDATESOURCEModel(this.BbDatasourceModel);
                //查询功图数据
                DataSet ds_gt = new DataSet();
                string sqlstr1 = " select * from GT  where TO_CHAR(GT.CSRQ,'YYYY-MM-DD')<='" + csrq + "'AND JH='"+jh+"' AND ROWNUM<=1 order by CSRQ DESC";
                ds_gt = datesoursebll.GetList_SQL(sqlstr1);
                table_gt = ds_gt.Tables[0];
                if (table_gt.Rows.Count == 0)
                {
                    dmzh = "00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00";
                    dmwy = "00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00,00.00";
                }
                else
                {
                    dmzh = table_gt.Rows[0]["DMZH"].ToString();
                    dmwy = table_gt.Rows[0]["DMWY"].ToString();

                }
                dla = table_gl.Rows[i]["DLA"].ToString();
                dlb = table_gl.Rows[i]["DLB"].ToString();
                dlc = table_gl.Rows[i]["DLC"].ToString();

                dya = table_gl.Rows[i]["DYA"].ToString();
                dyb = table_gl.Rows[i]["DYB"].ToString();
                dyc = table_gl.Rows[i]["DYC"].ToString();

                yggla = table_gl.Rows[i]["YGGLA"].ToString();
                ygglb = table_gl.Rows[i]["YGGLB"].ToString();
                ygglc = table_gl.Rows[i]["YGGLC"].ToString();

                wggla = table_gl.Rows[i]["WGGLA"].ToString();
                wgglb = table_gl.Rows[i]["WGGLB"].ToString();
                wgglc = table_gl.Rows[i]["WGGLC"].ToString();
                for (int j = 0; j < 144; j++)
                {
                    if (j < 143)
                    {
                        CJSJModel model = new CJSJModel();
                        //电流
                        index = dla.IndexOf(",");
                        substr1 = dla.Substring(0, index);
                        model.ADL = Convert.ToSingle(substr1);
                        dla = dla.Substring(index + 1);

                        index = dlb.IndexOf(",");
                        substr1 = dlb.Substring(0, index);
                        model.BDL = Convert.ToSingle(substr1);
                        dlb = dlb.Substring(index + 1);

                        index = dlc.IndexOf(",");
                        substr1 = dlc.Substring(0, index);
                        model.CDL = Convert.ToSingle(substr1);
                        dlc = dlc.Substring(index + 1);

                        //电压
                        index = dya.IndexOf(",");
                        substr1 = dya.Substring(0, index);
                        model.ADY = Convert.ToSingle(substr1);
                        dya = dya.Substring(index + 1);

                        index = dyb.IndexOf(",");
                        substr1 = dyb.Substring(0, index);
                        model.BDY = Convert.ToSingle(substr1);
                        dyb = dyb.Substring(index + 1);

                        index = dyc.IndexOf(",");
                        substr1 = dyc.Substring(0, index);
                        model.CDY = Convert.ToSingle(substr1);
                        dyc = dyc.Substring(index + 1);

                        //有功功率
                        index = yggla.IndexOf(",");
                        substr1 = yggla.Substring(0, index);
                        model.AYGGL = Convert.ToSingle(substr1);
                        yggla = yggla.Substring(index + 1);

                        index = ygglb.IndexOf(",");
                        substr1 = ygglb.Substring(0, index);
                        model.BYGGL = Convert.ToSingle(substr1);
                        ygglb = ygglb.Substring(index + 1);

                        index = ygglc.IndexOf(",");
                        substr1 = ygglc.Substring(0, index);
                        model.CYGGL = Convert.ToSingle(substr1);
                        ygglc = ygglc.Substring(index + 1);

                        model.HYGGL = model.AYGGL + model.BYGGL + model.CYGGL;//合相有功功率

                     
                        //无功功率
                        index = wggla.IndexOf(",");
                        substr1 = wggla.Substring(0, index);
                        model.AWGGL = Convert.ToSingle(substr1);
                        wggla = wggla.Substring(index + 1);

                        index = wgglb.IndexOf(",");
                        substr1 = wgglb.Substring(0, index);
                        model.BWGGL = Convert.ToSingle(substr1);
                        wgglb = wgglb.Substring(index + 1);

                        index = wgglc.IndexOf(",");
                        substr1 = wgglc.Substring(0, index);
                        model.CWGGL = Convert.ToSingle(substr1);
                        wgglc = wgglc.Substring(index + 1);

                        model.HWGGL = model.AWGGL + model.BWGGL + model.CWGGL;

                        //视在功率
                        model.ASZGL = model.ADY * model.ADL;
                        model.BSZGL = model.BDY * model.BDL;
                        model.CSZGL = model.CDY * model.CDL;
                        model.HSZGL = model.ASZGL + model.BSZGL + model.CSZGL;

                        //功率因数
                        model.AGLYS = model.AYGGL / (model.ASZGL);
                        model.BGLYS = model.BYGGL / (model.BSZGL);
                        model.CGLYS = model.CYGGL / (model.CSZGL);
                        model.HGLYS = model.HYGGL / (model.HSZGL);
                        //功图参数
                        index = dmzh.IndexOf(",");
                        substr1 = dmzh.Substring(0, index);
                        model.XDZH = Convert.ToSingle(substr1);
                        dmzh = dmzh.Substring(index + 1);

                        index = dmwy.IndexOf(",");
                        substr1 = dmwy.Substring(0, index);
                        model.LTWY = Convert.ToSingle(substr1);
                        dmwy = dmwy.Substring(index + 1);


                        model = transSjcjModel(model);
                        this.cjsjModels.Add(model);//将单条记录添加到集合中
                    }
                    else
                    {
                        CJSJModel model = new CJSJModel();
                        //电流
                        substr1 = dla.Substring(0);
                        model.ADL = Convert.ToSingle(substr1);

                        substr1 = dlb.Substring(0);
                        model.BDL = Convert.ToSingle(substr1);

                        substr1 = dlc.Substring(0);
                        model.CDL = Convert.ToSingle(substr1);

                        //电压

                        substr1 = dya.Substring(0);
                        model.ADY = Convert.ToSingle(substr1);

                        substr1 = dyb.Substring(0);
                        model.BDY = Convert.ToSingle(substr1);

                        substr1 = dyc.Substring(0);
                        model.CDY = Convert.ToSingle(substr1);

                        //有功功率

                        substr1 = yggla.Substring(0);
                        model.AYGGL = Convert.ToSingle(substr1);

                        substr1 = ygglb.Substring(0);
                        model.BYGGL = Convert.ToSingle(substr1);

                        substr1 = ygglc.Substring(0);
                        model.CYGGL = Convert.ToSingle(substr1);
                        model.HYGGL = model.AYGGL + model.BYGGL + model.CYGGL;//合相有功功率
                        //无功功率

                        substr1 = wggla.Substring(0);
                        model.AWGGL = Convert.ToSingle(substr1);

                        substr1 = wgglb.Substring(0);
                        model.BWGGL = Convert.ToSingle(substr1);

                        substr1 = wgglc.Substring(0);
                        model.CWGGL = Convert.ToSingle(substr1);
                        model.HWGGL = model.AWGGL + model.BWGGL + model.CWGGL;

                        //视在功率
                        model.ASZGL = model.ADY * model.ADL;
                        model.BSZGL = model.BDY * model.BDL;
                        model.CSZGL = model.CDY * model.CDL;
                        model.HSZGL = model.ASZGL +model.BSZGL + model.CSZGL;

                        //功率因数
                        model.AGLYS = model.AYGGL / (model.ASZGL);
                        model.BGLYS = model.BYGGL / (model.BSZGL);
                        model.CGLYS = model.CYGGL / (model.CSZGL);
                        model.HGLYS = model.HYGGL / (model.HSZGL);

                        //功图参数
                     
                        substr1 = dmzh.Substring(0);
                        model.XDZH = Convert.ToSingle(substr1);

                        substr1 = dmwy.Substring(0);
                        model.LTWY = Convert.ToSingle(substr1);

                        model = transSjcjModel(model);
                        this.cjsjModels.Add(model);//将单条记录添加到集合中
                    }

                }

                this.BbDatasourceModel.CJSJ = BinarySerializer.SerializeObject(this.cjsjModels);//将采集集合转换成二进制集合
                BDDATESOURSEBLL BdDataSourseBll = new BDDATESOURSEBLL();
                BDValueFunc valueFunc = new BDValueFunc();
                if (table_gt.Rows.Count < 1)
                {
                    valueFunc.setValue_TEMP(this.BbDatasourceModel, 0, 0);
                }
                else
                {
                    string cc_str = table_gt.Rows[0]["CC"].ToString();
                    string cs_str = table_gt.Rows[0]["CS"].ToString();
                    float cc_f = float.Parse(cc_str);
                    float cs_f = float.Parse(cs_str);
                    valueFunc.setValue_TEMP(this.BbDatasourceModel, cc_f, cs_f);
                }
                BdDataSourseBll.Add(this.BbDatasourceModel);

            }
            JScript.Alert(this.Page, "数据导入成功");
        }

        /// <summary>
        /// 将采集动态数据经过转换算法转换后形成真正能入库的数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CJSJModel transSjcjModel(CJSJModel model)
        {

            model.AYGGL = (model.AYGGL == null) ? 9999999 : model.AYGGL.Value;
            model.BYGGL = (model.BYGGL == null) ? 9999999 : model.BYGGL.Value;
            model.CYGGL = (model.CYGGL == null) ? 9999999 : model.CYGGL.Value;
            model.PL = (model.PL == null) ? 9999999 : model.PL.Value;
            model.HYGGL = (model.HYGGL == null) ? 9999999 : model.HYGGL.Value;
            model.AWGGL = (model.AWGGL == null) ? 9999999 : model.AWGGL.Value;
            model.BWGGL = (model.BWGGL == null) ? 9999999 : model.BWGGL.Value;
            model.CWGGL = (model.CWGGL == null) ? 9999999 : model.CWGGL.Value;
            model.HWGGL = (model.HWGGL == null) ? 9999999 : model.HWGGL.Value;
            model.ADY = (model.ADY == null) ? 9999999 : model.ADY.Value;
            model.BDY = (model.BDY == null) ? 9999999 : model.BDY.Value;
            model.CDY = (model.CDY == null) ? 9999999 : model.CDY.Value;
            model.HDY = (model.HDY == null) ? 9999999 : model.HDY.Value;
            model.ADL = (model.ADL == null) ? 9999999 : model.ADL.Value;
            model.BDL = (model.BDL == null) ? 9999999 : model.BDL.Value;
            model.CDL = (model.CDL == null) ? 9999999 : model.CDL.Value;
            model.HDL = (model.HDL == null) ? 9999999 : model.HDL.Value;
            model.AGLYS = (model.AGLYS == null) ? 9999999 : model.AGLYS.Value;
            model.BGLYS = (model.BGLYS == null) ? 9999999 : model.BGLYS.Value;
            model.CGLYS = (model.CGLYS == null) ? 9999999 : model.CGLYS.Value;
            model.HGLYS = (model.HGLYS == null) ? 9999999 : model.HGLYS.Value;
            model.ASZGL = (model.ASZGL == null) ? 9999999 : model.ASZGL.Value;
            model.BSZGL = (model.BSZGL == null) ? 9999999 : model.BSZGL.Value;
            model.CSZGL = (model.CSZGL == null) ? 9999999 : model.CSZGL.Value;
            model.HSZGL = (model.HSZGL == null) ? 9999999 : model.HSZGL.Value;
            model.DJSCNJ = (model.DJSCNJ == null) ? 9999999 : model.DJSCNJ.Value;
            model.DJSCZS = (model.DJSCZS == null) ? 9999999 : model.DJSCZS.Value;
            model.DJXL = (model.DJXL == null) ? 9999999 : model.DJXL.Value;
            model.JSJSCNJ = (model.JSJSCNJ == null) ? 9999999 : model.JSJSCNJ.Value;
            model.QBZJ = (model.QBZJ == null) ? 9999999 : model.QBZJ.Value;
            model.LTWY = (model.LTWY == null) ? 9999999 : model.LTWY.Value;
            model.XDZH = (model.XDZH == null) ? 9999999 : model.XDZH.Value;
            model.YY = (model.YY == null) ? 9999999 : model.YY.Value;
            model.TY = (model.TY == null) ? 9999999 : model.TY.Value;
            model.YMGD = (model.YMGD == null) ? 9999999 : model.YMGD.Value;
            model.WD = (model.WD == null) ? 9999999 : model.WD.Value;
            model.YGNH = (model.YGNH == null) ? 9999999 : model.YGNH.Value;
            model.WGNH = (model.WGNH == null) ? 9999999 : model.WGNH.Value;

            return model;
        }

        /// <summary>
        /// 将采集静态数据经过转换算法转换后形成真正能入库的数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public BDDATESOURCEModel transBDDATESOURCEModel(BDDATESOURCEModel model)
        {
            model.WELLID = cjsjParser.getWELLIDParser(model.WELLID);
            model.JCRQ = cjsjParser.getJCRQParser(model.JCRQ);
            model.KSSJ = cjsjParser.getKSSJParser(model.KSSJ);
            model.JSSJ = cjsjParser.getJSSJParser(model.JSSJ);
            model.CJZSJ = cjsjParser.getCJZSJParser(model.CJZSJ);
            model.CJPL = cjsjParser.getCJPLParser(model.CJPL);
            model.CJZDS = 144;
            model.SCCDS = cjsjParser.getSCCDSParser(model.SCCDS);
            model.XCCDS = cjsjParser.getXCCDSParser(model.XCCDS);
            model.PULLID = cjsjParser.getPULLIDParser(model.PULLID);
            model.DJID = cjsjParser.getDJIDParser(model.DJID);
            model.PDGID = cjsjParser.getPDGIDParser(model.PDGID);
            model.GLDW = cjsjParser.getGLDWParser(model.GLDW);
            model.JCDW = cjsjParser.getJCDWParser(model.JCDW);
            model.CSR = cjsjParser.getCSRParser(model.CSR);
            model.CJSBXH = cjsjParser.getCJSBXHParser(model.CJSBXH);
            model.CJSBBB = cjsjParser.getCJSBBBParser(model.CJSBBB);
            model.SCRJBB = cjsjParser.getSCRJBBParser(model.SCRJBB);
            return model;
        }

    }
}